package ods_log

import (
	"server/global"
	"server/model/common/request"
)

type OdsDbSyncLogService struct {
}


func (odsDbSyncLogService *OdsDbSyncLogService) CreateOdsDbSyncLog(odsDbSyncLog *OdsDbSyncLog) (err error) {
	err = global.SysDB.Create(odsDbSyncLog).Error
	return err
}


func (odsDbSyncLogService *OdsDbSyncLogService) DeleteOdsDbSyncLog(odsDbSyncLog OdsDbSyncLog) (err error) {
	err = global.SysDB.Delete(&odsDbSyncLog).Error
	return err
}


func (odsDbSyncLogService *OdsDbSyncLogService) DeleteOdsDbSyncLogByIds(ids request.IdsReq) (err error) {
	err = global.SysDB.Delete(&[]OdsDbSyncLog{}, "id in ?", ids.Ids).Error
	return err
}


func (odsDbSyncLogService *OdsDbSyncLogService) UpdateOdsDbSyncLog(odsDbSyncLog OdsDbSyncLog) (err error) {
	err = global.SysDB.Save(&odsDbSyncLog).Error
	return err
}


func (odsDbSyncLogService *OdsDbSyncLogService) GetOdsDbSyncLogById(id uint) (odsDbSyncLog OdsDbSyncLog, err error) {
	err = global.SysDB.Where("id = ?", id).First(&odsDbSyncLog).Error
	return
}


func (odsDbSyncLogService *OdsDbSyncLogService) GetOdsDbSyncLogInfoList(info OdsDbSyncLogSearch) (list []OdsDbSyncLog, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.SysDB.Model(&OdsDbSyncLog{})
	var odsDbSyncLogs []OdsDbSyncLog
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Limit(limit).Offset(offset).Find(&odsDbSyncLogs).Error
	return odsDbSyncLogs, total, err
}


func (odsDbSyncLogService *OdsDbSyncLogService) GetAllOdsDbSyncLogs() (odsDbSyncLog []OdsDbSyncLog, err error) {
	err = global.SysDB.Find(&odsDbSyncLog).Error
	return odsDbSyncLog, err
}
